package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.dto.StartSpiderDto;
import com.ruoyi.system.domain.standard_relevant.Standard;
import com.ruoyi.system.domain.standard_relevant.StandardExcel;
import com.ruoyi.system.service.standard.IStandardService;
import com.ruoyi.system.service.standard.impl.StandardServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.StandardSpider;
import com.ruoyi.system.service.IStandardSpiderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据采集Controller
 *
 * @author ruoyi
 * @date 2024-09-21
 */
@RestController
@RequestMapping("/stdspider/spider")
public class StandardSpiderController extends BaseController
{
    @Autowired
    private IStandardSpiderService standardSpiderService;

    @Autowired
    private IStandardService standardService;

    /**
     * 查询数据采集列表
     */
    @PreAuthorize("@ss.hasPermi('stdspider:spider:list')")
    @GetMapping("/list")
    public TableDataInfo list(StandardSpider standardSpider)
    {
        startPage();
        List<StandardSpider> list = standardSpiderService.selectStandardSpiderList(standardSpider);
        return getDataTable(list);
    }

    /**
     * 导出数据采集列表
     */
    @PreAuthorize("@ss.hasPermi('stdspider:spider:export')")
    @Log(title = "数据采集", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StandardSpider standardSpider)
    {
        List<StandardSpider> list = standardSpiderService.selectStandardSpiderList(standardSpider);
        ExcelUtil<StandardSpider> util = new ExcelUtil<StandardSpider>(StandardSpider.class);
        util.exportExcel(response, list, "数据采集数据");
    }

    /**
     * 获取数据采集详细信息
     */
    @PreAuthorize("@ss.hasPermi('stdspider:spider:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(standardSpiderService.selectStandardSpiderById(id));
    }

    /**
     * 新增数据采集
     */
//    @PreAuthorize("@ss.hasPermi('stdspider:spider:add')")
    @Log(title = "数据采集", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StandardSpider standardSpider)
    {
        return toAjax(standardSpiderService.insertStandardSpider(standardSpider));
    }
    /**
     * 调用python进行采集
     */
    @PreAuthorize("@ss.hasPermi('stdspider:spider:add')")
    @PostMapping("/start")
    public AjaxResult startSpider(@RequestBody StartSpiderDto startSpiderDto) {


        /**
         * 调用异步方法，进行数据处理 ---》 异步方法中处理
         */
        standardSpiderService.calSpider(startSpiderDto);
        // 返回“数据分析中”的状态
        return AjaxResult.success("数据爬取任务已提交，请稍后检查状态。");
    }
    /**
     * 修改数据采集
     */
//    @PreAuthorize("@ss.hasPermi('stdspider:spider:edit')")
    @Log(title = "数据采集", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StandardSpider standardSpider)
    {
        return toAjax(standardSpiderService.updateStandardSpider(standardSpider));
    }

    /**
     * 审核数据采集
     */
    @PreAuthorize("@ss.hasPermi('stdspider:spider:edit')")
    @Log(title = "数据采集", businessType = BusinessType.UPDATE)
    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody StandardSpider standardSpider)
    {
        return toAjax(standardSpiderService.auditStandardSpider(standardSpider));
    }


    /**
     * 删除数据采集
     */
    @PreAuthorize("@ss.hasPermi('stdspider:spider:remove')")
    @Log(title = "数据采集", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(standardSpiderService.deleteStandardSpiderByIds(ids));
    }

    @GetMapping("/standardList")
    public TableDataInfo standardList(Standard standard)
    {
//        startPage();
        List<Standard> list = standardService.selectStandardList(standard);
        return getDataTable(list);
    }
}
